package a04_字符串;

/**
 * <p>
 * a02_反转字符串2
 * </p>
 *
 * @author flyduck
 * @since 2024-07-09
 */
public class a02_反转字符串2复习2 {
    public static void main(String[] args) {
        System.out.println(reverseStr("abcdefg", 8));
    }


    public static String reverseStr(String source, int k) {
        char[] chars = source.toCharArray();
        for (int i = 0; i < chars.length; i = i + 2 * k) {
            if(i + k - 1 < chars.length){
                reverse(chars, i, i + k - 1);
            }else {
                reverse(chars, i, chars.length - 1);
            }
        }
        return new String(chars);
    }

    public static void reverse(char[] chars,int leftIdx,int rightIdx){
        while (leftIdx < rightIdx) {
            char temp = chars[leftIdx];
            chars[leftIdx] = chars[rightIdx];
            chars[rightIdx] = temp;
            leftIdx++;
            rightIdx--;
        }
    }
}
